<?php
/**
 * 普通分页
 * Meta值：ParamName,ParamMethod,CurrentPage,PageSize,RowCount,Limit_Start 
 */
class Pager_Common extends Pager
{
    public function Build(&$data,$params=NULL)
    {        
        #获取Meta数据
        $attr=($data['Meta']==NULL ? $data:$data['Meta']);
        $currentPage=$attr['CurrentPage'];$rowsCount=$attr['RowCount'];$pageCount=$attr['PageCount'];$pageSize=$attr['PageSize'];

        #获取参数
        $parameters=NULL;$isDetail=NULL;$curtomTags=NULL;$rewriter=NULL;$url=NULL;
        if($params) {
            $url=@$params['URL'];$parameters=@$params['Parameters'];$isDetail=@$params['IsDetail'];$curtomTags=@$params['CurtomTags'];$rewriter=@$params['Rewriter'];
        }

        #分页默认模板
        $tags=array('pre'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页','detail'=>'页次：pageCode页 共pageCount页 总计'.$rowsCount.'条 toPagecode');
        if($curtomTags){
            $tags=$curtomTags;
        }

        #开始处理
        $tempStr='';
        if($rowsCount)
        {
            //解析参数
            $tempParams='';
            if($parameters!=NULL)
            {
                foreach($parameters as $key=>$value){
                    $tempParams.=($key.'='.$value.'&');
                }
                $tempParams=substr($tempParams, 0, strlen($tempParams)-1);
            }
            $tempParams=($tempParams=='' ? '':'&'.$tempParams);            

            //生成链接
            if($url)
            {
                if(strpos($url,'?')!==FALSE){
                    $url=$url.'&page=';
                }
                else{
                    $url=$url.'?page=';
                }
            }
            $link_first=$url.'1'.$tempParams;
            $link_pre=$url.($currentPage-1).$tempParams;
            $link_next=$url.($currentPage+1).$tempParams;
            $link_last=$url.$pageCount.$tempParams;
            if($rewriter)
            {
                $link_pre=$rewriter->Get($link_pre);
                $link_first=$rewriter->Get($link_first);
                $link_next=$rewriter->Get($link_next);
                $link_last=$rewriter->Get($link_last);
            }

            //处理显示
            if($currentPage==1&&$pageCount>1){
                $tempStr='<span style="color:gray;">'.$tags['first'].'</span> <span style="color:gray;">'.$tags['pre'].'</span> <a href="'.$link_next.'">'.$tags['next'].'</a> <a href="'.$link_last.'">'.$tags['last'].'</a> ';
            }
            else if($currentPage==$pageCount&&$pageCount>1){
                $tempStr='<a href="'.$link_first.'">'.$tags['first'].'</a> <a href="'.$link_pre.'">'.$tags['pre'].'</a> <span style="color:gray;">'.$tags['next'].'</span> <span style="color:gray;">'.$tags['last'].'</span> ';
            }
            else if($currentPage==$pageCount&&$pageCount==1){
                $tempStr='<span style="color:gray;">'.$tags['first'].'</span> <span style="color:gray;">'.$tags['pre'].'</span> <span style="color:gray;">'.$tags['next'].'</span> <span style="color:gray;">'.$tags['last'].'</span> ';
            }
            else{
                $tempStr='<a href="'.$link_first.'">'.$tags['first'].'</a> <a href="'.$link_pre.'">'.$tags['pre'].'</a> <a href="'.$link_next.'">'.$tags['next'].'</a> <a href="'.$link_last.'">'.$tags['last'].'</a> ';
            }

            //详细信息
            if($isDetail==TRUE)
            {
                $link_jupm=$url.'"+__PHP__PagerCommon_Element.value+"'.$tempParams;
                if($rewriter){
                    $link_jupm=$rewriter->Get($link_jupm);
                }

                $htmlSelectControl='<input type="text" name="__PHP_PagerCommon_PageCode" id="__PHP_PagerCommon_PageCode" style="width:20px; height:10px; line-height:10px;" maxlength="4" /> ';
                $htmlSelectControl.='<span style="cursor:pointer;" onclick="__PHP_PagerCommon_Jump();">跳转</span>';
                $htmlSelectControl.='<script type="text/javascript">';
                $htmlSelectControl.='function __PHP_PagerCommon_Jump()';
                $htmlSelectControl.='{';
                $htmlSelectControl.='var __PHP__PagerCommon_Element=document.getElementById("__PHP_PagerCommon_PageCode");';
                $htmlSelectControl.='if(__PHP__PagerCommon_Element.value==""||isNaN(__PHP__PagerCommon_Element.value))';
                $htmlSelectControl.='__PHP__PagerCommon_Element.select();';
                $htmlSelectControl.='else ';
                $htmlSelectControl.='window.location="'.$link_jupm.'"';
                $htmlSelectControl.='}';
                $htmlSelectControl.='</script>';
                $tempStr.=' ';
                $tempStr.=$tags['detail'];
                $tempStr=preg_replace('/pageCode/', $currentPage.'/'.$pageCount, $tempStr);
                $tempStr=preg_replace('/pageCount/', $pageCount, $tempStr);
                $tempStr=preg_replace('/toPagecode/', $htmlSelectControl, $tempStr);
            }            
        }        
        return $tempStr;
    }
}
?>
